package com.wpx.fileSys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;

import com.wpx.fileSys.dao.UserDao;
import com.wpx.fileSys.utils.JdbcUtils;
import com.wpx.fileSys.utils.Token;


public class UserDaoImpl implements UserDao {

	@Override
	public JSONObject loginSys(String username, String pwd) {
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql="select  userName,token from User where userName=? and pwd=?";

		JSONObject jsonObject=new JSONObject();
		jsonObject.put("userName", username);
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, pwd);
			rs=pstm.executeQuery();
			rs.last();
			if (rs.getRow()==0) {
				jsonObject.put("msg", "密码错误");
				System.out.println("error 账号密码错误");
			}
			rs.beforeFirst();
			while(rs.next()){
				Token tokenInfo=new Token();
				String token=tokenInfo.getToken();
				updateToken(username, token);
				System.out.println("token+"+token);
				jsonObject.put("token", token);
				jsonObject.put("msg", "0");						
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 异常错误");
		}finally{
			JdbcUtils.close(rs, pstm, conn);
		}
		
		return jsonObject;
	}

	private void updateToken(String username, String token) {
		// TODO Auto-generated method stub
		String sql = "update User set token = ?,last_time=? where userName= ?";
		Connection connection = JdbcUtils.getConnection();
		PreparedStatement preparedStatement = null;
		Date now=new Date();
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, token);
			preparedStatement.setString(3, username);
			preparedStatement.setString(2, dateFormat.format(now));
			
			preparedStatement.executeUpdate();
			System.out.println("更新token成功");
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println("error 更新token失败");
			} finally {
				JdbcUtils.close(null, preparedStatement, connection);
			}	
		
	}

	@Override
	public Boolean tokenSys(String username, String token) {
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql="select  userName,token from User where userName=? and token=?";

		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, token);
			rs=pstm.executeQuery();
			
			if (rs.next()) {
				System.out.println("验证token成功");
				return true;
			}else {
				System.out.println("验证token失败");
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("error 验证token失败");
		} finally {
			JdbcUtils.close(null, pstm, conn);
		}
		return false;
		
	}
	


}
